import {observable, action, toJS} from 'mobx';
import {WeaTableNew, WeaForm} from 'comsMobx';
import {WeaLocaleProvider} from "ecCom";
import {message, Modal} from "antd";
import _ from 'lodash';
import * as contractManagementApi from '../api/ContractManagementApi';
import {indexOf, isBlank, parseIntValue} from '../util/CommonUtil';
import LanguageConstant from '../constant/LanguageConstant';

const {getLabel} = WeaLocaleProvider;
const confirm = Modal.confirm;

class ContractNameRuleStore {

    /**
     * 是否有操作页面的权限
     *
     * @type {boolean}
     */
    @observable authorized = true;

    /**
     * 加载状态
     */
    @observable loading = false;


    /**
     * 设置id
     *
     * @type {number}
     */
    @observable settingId = -1;

    /**
     * 是否是设置合同默认命名规则
     *
     * @type {boolean}
     */
    @observable isDefault = false;

    /**
     * 命名规则数据
     *
     * @type {Array}
     */
    @observable nameRules = [];

    /**
     * 相关的下拉选择框选项
     *
     * @type {{}}
     */
    @observable selectOptions = {};

    /**
     * 初始化
     *
     * @param params
     */
    @action
    init = (params = {}) => {
        const {settingId, isDefault} = params;
        this.settingId = settingId;
        this.isDefault = isDefault;
    }

    /**
     * 获取命名规则表
     *
     * @param params
     */
    @action
    getNameRulesTableDatas = (params = {}) => {
        const settingId = toJS(this.settingId);
        const isDefault = toJS(this.isDefault);
        params = {settingId, isDefault, ...params};
        contractManagementApi.getContractNameRules(params).then(res => {
            const {code, data} = res;
            if (code == 200) {
                const {selectOptions, nameRules} = data;
                this.nameRules = nameRules;
                this.selectOptions = selectOptions;
            }
        });
    }


    /**
     * 添加数据
     *
     * @param datas
     * @param addData
     * @returns {*}
     */
    @action
    willAddNameRule = (datas, addData) => {
        const defaultData = {type: 1};
        const nameRules = toJS(this.nameRules);
        addData = {...addData, ...defaultData};
        nameRules.push(addData);
        this.nameRules = nameRules;
        return nameRules;
    }

    /**
     * 删除规则
     *
     * @param datas
     * @param keys
     * @returns {Promise<any>}
     */
    @action
    willDeleteNameRule = (datas, keys) => {
        return new Promise((resolve, reject) => {
            resolve(true);
        });
    }

    /**
     * 表格数据发生变化
     *
     * @param data
     */
    onNameRuleChange = (data) => {
        this.nameRules = data;
    }

    /**
     * 保存合同命名规则数据
     *
     * @param successCallback
     */
    @action
    saveNameRules = (params = {}, beforeRequestCallback, afterResponseCallback) => {
        if (beforeRequestCallback && _.isFunction(beforeRequestCallback)) {
            beforeRequestCallback();
        }
        const nameRules = JSON.stringify(toJS(this.nameRules));
        const settingId = toJS(this.settingId);
        const isDefault = toJS(this.isDefault);
        params = {...params, settingId, isDefault, nameRules}
        contractManagementApi.saveContractNameRules(params).then(res => {
            const {code} = res;
            if (code == 200) {
                message.success(getLabel(LanguageConstant.SAVE_SUCCESS, "保存成功"));
            } else {
                message.error(getLabel(LanguageConstant.SAVE_FAIL, "保存失败"));
            }
            if (afterResponseCallback && _.isFunction(afterResponseCallback)) {
                afterResponseCallback();
            }
        });
    }

}

export default ContractNameRuleStore;